package logic

import (
	"context"
	"errors"
	"login-hub/model/mysql"

	"login-hub/loginrpc/internal/svc"
	"login-hub/loginrpc/loginrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodAddLogic {
	return &GoodAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodAddLogic) GoodAdd(in *loginrpc.GoodAddRequest) (*loginrpc.GoodAddResponse, error) {
	// todo: add your logic here and delete this line

	var good mysql.Good
	mysql.DB.Debug().Where("name = ?", in.Name).Find(&good)
	if good.ID != 0 {
		return nil, errors.New("当前商品已存在")
	}

	good = mysql.Good{
		Name:  in.Name,
		Price: float64(in.Price),
		Uid:   int(in.Uid),
	}
	mysql.DB.Debug().Create(&good)

	return &loginrpc.GoodAddResponse{
		GId: int64(good.ID),
	}, nil
}
